<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="cate.top" :to="`/category/${cate.top.id}`">{{cate.top.name}}</XtxBreadItem>
    <transition name="fade-right" mode="out-in">
      <XtxBreadItem v-if='cate.sub' :key='cate.sub.name'>{{cate.sub.name}}</XtxBreadItem>
    </transition>
  </XtxBread>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
export default {
  name: 'SubBread',
  setup () {
    const store = useStore()
    const route = useRoute()
    const cate = computed(() => {
      const tempCate = {}
      store.state.category.list.forEach((top) => {
        if (top.children) {
          top.children.forEach((sub) => {
            // 判断id是否相同
            if (sub.id === route.params.id) {
              // 一级类目
              tempCate.top = {
                id: top.id,
                name: top.name
              }
              // 二级类目
              tempCate.sub = {
                id: sub.id,
                name: sub.name
              }
            }
          })
        }
      })
      return tempCate
    })
    return { cate }
  }
}
</script>
